<!-- 收货对账作业：蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" @clickNumber="clickNumber" />
    <Find :searchImg="false" :OriginalFilter="{ static: true }" :formList="formList2" @findClicked="findClicked2" />

    <el-card style="margin-top: 10px" shadow="always" :body-style="{ padding: '5px' }">
      <h1>入库明细查询</h1>
      <Table
        @selectList="selectTableList"
        selection
        :tableHeader="tableHeader"
        :tableData="tableData"
        @currentRow="currentRow"
        :summaryData="summaryData"
        summary
        :isLoading="isLoading"
      />
    </el-card>
    <el-card shadow="never" :body-style="{ padding: '5px' }">
      <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import router from '@/router'
import LoginWork from '@/utils/LoginWork'
import { selectList } from '@/plugins/mixin'
import fileRequest from '@/utils/fileRequest'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'HarvestReconciliationWork'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'
import { ref } from 'vue'

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '订购单号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '送货单号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '对账月份',
    dateValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '品牌',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '指令',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '送货日期',
    dateValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '材料名称',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '对账单号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '财务备注',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '采购备注',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '类型',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'radio',
    checkboxLabel: '未检核未输入',
    label: '1'
  },
  {
    type: 'radio',
    checkboxLabel: '未检核已输入',
    label: '2'
  },
  {
    type: 'radio',
    checkboxLabel: '已检核未对账',
    label: '3'
  },
  {
    type: 'radio',
    checkboxLabel: '已检核已对账',
    label: '4'
  },
  {
    type: 'radio',
    checkboxLabel: '全部',
    label: '5'
  }
])
const formList2 = ref([
  {
    type: 'input',
    inputPlaceholder: '对账月份',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '打勾记录对账'
  },
  {
    type: 'button',
    checkboxLabel: '打勾记录取消对账'
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '结算厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '备注信息',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '结算厂商/备注'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出明细'
  },
  {
    type: 'button',
    checkboxLabel: '转费用结算作业'
  }
])

const tableHeader = ref([
  {
    label: '类型',
    prop: 'purType'
  },
  {
    label: '对账月份',
    prop: 'financeRecYymm'
  },
  {
    label: '对账单号',
    prop: 'balanceNo'
  },
  {
    label: '订购单号',
    prop: 'purNo'
  },
  {
    label: '订购日期',
    prop: 'purDate'
  },
  {
    label: '送货日期',
    prop: 'sendDate'
  },
  {
    label: '送货单号',
    prop: 'sendNo'
  },
  {
    label: '厂商名称',
    prop: 'vendorName'
  },
  {
    label: '结算厂商',
    prop: 'vendorName2'
  },
  {
    label: '品牌',
    prop: 'brand'
  },
  {
    label: '指令',
    prop: 'orderId'
  },
  {
    label: '型体',
    prop: 'style'
  },
  {
    label: '材料颜色',
    prop: 'mtlColor'
  },
  {
    label: '材料名称',
    prop: 'mtlName'
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    label: '用量',
    prop: 'usage'
  },
  {
    label: '订购数量',
    prop: 'purQty'
  },
  {
    label: '收货数量',
    prop: 'sendQty'
  },
  {
    label: '折扣单价',
    prop: 'price'
  },
  {
    label: '金额',
    prop: 'amt'
  },
  {
    label: '财务备注',
    prop: 'financeRecMemo'
  },
  {
    label: '原单价',
    prop: 'orgPrice'
  },
  {
    label: '采购备注',
    prop: 'remark'
  },
  {
    label: '备注信息',
    prop: 'financeAccMemo'
  },
  {
    label: '材料名称',
    prop: 'sampleType'
  },
  {
    label: '指令',
    prop: 'orderQty'
  },
  {
    label: '样品类型',
    prop: 'dutyMemo'
  },
  {
    label: '责任人',
    prop: 'dutyUser'
  },
  {
    label: '责任原因',
    prop: 'dutyRek'
  }
])
// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
}
const findClicked2 = async (content: string) => {
  if (content === '置空') {
    formList2.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '转费用结算作业') {
    router.push('/GeneralFinanceDepartment/RongyangFinancialOperations/ExpenseSettlementOperation')
  }
  if (content === '打勾记录对账') {
    if (selectedLists.value.length === 0) {
      JMessage.error('请选择数据')
      return
    }
    const month = formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '对账月份'
    )?.inputValue
    if (!month) {
      JMessage.error('请填写对账月份')
      return
    }
    const res = (await LoginWork.put(
      '/ryFinance/mtlPreOtherInUp',
      selectedLists.value.map((item: any) => {
        item.financeFlag = 1
        item.financeRecYymmNew = month
        item.purTypeOld = item.purType
        return item
      })
    )) as any
    if (res.code === 200) {
      JMessage.success('对账成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
    console.log(res)
  }
  if (content === '打勾记录取消对账') {
    if (selectedLists.value.length === 0) {
      JMessage.error('请选择数据')
      return
    }
    const month = formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '对账月份'
    )?.inputValue
    if (!month) {
      JMessage.error('请填写对账月份')
      return
    }
    const res = (await LoginWork.put(
      '/ryFinance/mtlPreOtherInUp',
      selectedLists.value.map((item: any) => {
        item.purTypeOld = item.purType
        item.financeRecYymmNew = month
        item.financeFlag = 0
        return item
      })
    )) as any
    if (res.code === 200) {
      JMessage.success('取消对账成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '结算厂商/备注') {
    if (selectedLists.value.length === 0) {
      JMessage.error('请选择数据')
      return
    }
    const vendorNo = formList2.value.find(
      (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '结算厂商'
    )?.selectValue
    // 备注信息
    const remark = formList2.value.find(
      (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '备注信息'
    )?.selectValue
    if (vendorNo && !remark) {
      const res = (await LoginWork.put(
        '/ryFinance/mtlPreOtherInUp',
        selectedLists.value.map((item: any) => {
          item.vendorNo2 = vendorNo
          item.vendorName2New = vendorNo.split(' ')[1]
          item.vendorNo2New = vendorNo.split(' ')[0]
          // item.financeAccMemoNew = remark
          item.purTypeOld = item.purType
          return item
        })
      )) as any
      if (res.code === 200) {
        JMessage.success('结算厂商成功')
        getTableList()
      } else {
        JMessage.error(res.message)
      }
    } else if (!vendorNo && remark) {
      const res = (await LoginWork.put(
        '/ryFinance/mtlPreOtherInUp',
        selectedLists.value.map((item: any) => {
          item.financeAccMemoNew = remark
          item.purTypeOld = item.purType
          return item
        })
      )) as any
      if (res.code === 200) {
        JMessage.success('备注修改成功')
        getTableList()
      } else {
        JMessage.error(res.message)
      }
    } else if (vendorNo && remark) {
      const res = (await LoginWork.put(
        '/ryFinance/mtlPreOtherInUp',
        selectedLists.value.map((item: any) => {
          item.vendorNo2 = vendorNo
          item.vendorName2New = vendorNo.split(' ')[1]
          item.vendorNo2New = vendorNo.split(' ')[0]
          item.financeAccMemoNew = remark
          item.purTypeOld = item.purType
          return item
        })
      )) as any
      if (res.code === 200) {
        JMessage.success('结算厂商和备注修改成功')
        getTableList()
      } else {
        JMessage.error(res.message)
      }
    } else {
      JMessage.error('请选择结算厂商或备注')
    }
  }
  if (content === '导出明细') {
    const res = (await fileRequest.post(`/ryFinance/downloadMtlPreOther`, {
      balanceNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '对账单号'
      )?.inputValue,
      brand: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
        ?.selectValue,
      financeRecMemo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '财务备注'
      )?.inputValue,
      financeRecYymm: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '对账月份'
      )?.inputValue,
      flag: cfmFlag.value,
      mtlName: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '材料名称'
      )?.inputValue,
      orderId: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令'
      )?.inputValue,
      purNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '订购单号'
      )?.inputValue,
      purType: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '类型')
        ?.selectValue,
      remark: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '采购备注'
      )?.inputValue,
      sendDateEnd: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '送货日期'
      )?.dateValue?.[1],
      sendDateStart: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '送货日期'
      )?.dateValue?.[0],
      sendNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '送货单号'
      )?.inputValue,
      vendorNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
        ?.selectValue,
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 19,
    num: 0
  },
  {
    index: 21,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
const cfmFlag = ref('1')
const clickNumber = (oldval: any) => {
  cfmFlag.value = oldval
  getTableList()
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/ryFinance/mtlPreOtherInQuery', {
    balanceNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '对账单号'
    )?.inputValue,
    brand: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
      ?.selectValue,
    financeRecMemo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '财务备注'
    )?.inputValue,
    financeRecYymm: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '对账月份'
    )?.inputValue,
    flag: cfmFlag.value,
    mtlName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '材料名称'
    )?.inputValue,
    orderId: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令'
    )?.inputValue,
    purNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '订购单号'
    )?.inputValue,
    purType: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '类型')
      ?.selectValue,
    remark: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '采购备注'
    )?.inputValue,
    sendDateEnd: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '送货日期'
    )?.dateValue?.[1],
    sendDateStart: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '送货日期'
    )?.dateValue?.[0],
    sendNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '送货单号'
    )?.inputValue,
    vendorNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
      ?.selectValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.dtos
  total.value = tableDataList.total
  currentRow()
  // summaryData.value[0].num = tableDataList.sum[0].orderCount
  // summaryData.value[1].num = tableDataList.sum[0].surplusCount
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)

// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
}

const selectedLists = ref([])
const selectTableList = (selectedList: any) => {
  selectedLists.value = selectedList
}

const getSelectList = async () => {
  await selectList(formList, () => service.get<any, any>(`/prod/getPcProdDynamicCondition`))
  let vendorNames = formList.value.find(
    (item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商'
  ) as any
  let vendorNames2 = formList2.value.find(
    (item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '结算厂商'
  ) as any
  let vendorName = await service.get<any, any>('/back/vendorQuery')
  if (vendorName) {
    vendorName.forEach((item: any, index: any) => {
      if (item === null) return
      vendorNames.selectOptions?.push({
        optionKey: index,
        optionValue: `${item.vendorNo}`,
        LeftLabel: `${item.vendorNo}`,
        RightLabel: `${item.vendorName}`
      })
      vendorNames2.selectOptions?.push({
        optionKey: index,
        optionValue: `${item.vendorNo} ${item.vendorName}`
      })
    })
  }
}
// 品牌下拉框
onMounted(async () => {
  await selectList(formList, () => service.get('/back/order/allOrderCondition'))
  getSelectList()
})
</script>

<style lang="less" scoped>
h1 {
  font-size: 16px;
}
</style>
